﻿Imports System.Data
Imports System.Data.SqlClient
Imports QuanLyCafe.NhanVien_DTO
'Imports QuanLyCafe.DataProvider

Public Class NhanVien_DAO
    Public Shared Function GetListNV_DAO()
        Dim str As String = "Select * From NHANVIEN"
        Dim conn As SqlConnection
        conn = DataProvider.Connection()
        Dim dt As DataTable
        Dim cmd As SqlCommand
        Dim dataApdap As SqlDataAdapter
        cmd = New SqlCommand(str, conn)
        dt = New DataTable
        dataApdap = New SqlDataAdapter
        dataApdap.SelectCommand = cmd
        dataApdap.Fill(dt)
        Return dt
    End Function

    Public Shared Sub InsertNV_DAO(ByVal nv As NhanVien_DTO)
        Dim conn As SqlConnection
        conn = DataProvider.Connection()
        ' Dim str As String = "Insert into NHANVIEN(MaNV,TenNV,CMND,Quequan,SoDT,Gioitinh,Ngaysinh,Chucvu) values (@MaNV , @TenNV , @CMND, @Quequan,@SoDT , @Gioitinh,@Ngaysinh,@Chucvu)"
        Dim str As String = "Insert into NHANVIEN(MaNV,TenNV,CMND,DiaChi,Gioitinh,Ngaysinh,Chucvu) values (@MaNV , @TenNV , @CMND, @DiaChi , @Gioitinh,@Ngaysinh,@Chucvu)"
        Dim cmd As SqlCommand = New SqlCommand(str, conn)
        cmd.Parameters.AddWithValue("@MaNV", nv.MaNhanVien.ToString())
        cmd.Parameters.AddWithValue("@TenNV", nv.TenNhanVien.ToString())
        cmd.Parameters.AddWithValue("@CMND", nv.CMNhanDan.ToString())
        cmd.Parameters.AddWithValue("@DiaChi", nv.QQuan.ToString())
        'cmd.Parameters.AddWithValue("@SoDT", nv.SDienThoai.ToString())
        cmd.Parameters.AddWithValue("@Gioitinh", nv.GTinh.ToString())
        cmd.Parameters.AddWithValue("@Ngaysinh", nv.NSinh.ToString())
        cmd.Parameters.AddWithValue("@Chucvu", nv.CVu.ToString())
        cmd.ExecuteNonQuery()
    End Sub

    Public Shared Sub UpdateNV_DAO(ByVal nv As NhanVien_DTO)
        Try
            Dim conn As SqlConnection
            conn = DataProvider.Connection()
            'Dim str As String = "Update NHANVIEN SET TenNV = @TeNV, CMND = @CMND, Quequan = @Quequan,SoDT = @SoDT,Gioitinh = @Gioitinh,Ngaysinh = @Ngaysinh,Chucvu = @Chucvu Where MaNV = @MaNV"
            Dim str As String = "Update NHANVIEN SET TenNV = @TeNV, CMND = @CMND, DiaChi = @DiaChi, Gioitinh = @Gioitinh,Ngaysinh = @Ngaysinh,Chucvu = @Chucvu Where MaNV = @MaNV"
            Dim cmd As SqlCommand = New SqlCommand()
            cmd.Connection = conn
            'cmd.CommandText = "Update NHANVIEN SET TenNV = @TeNV, CMND = @CMND, Quequan = @Quequan,SoDT = @SoDT,Gioitinh = @Gioitinh,Ngaysinh = @Ngaysinh,Chucvu = @Chucvu Where MaNV = @MaNV"
            cmd.Parameters.AddWithValue("@MaNV", nv.MaNhanVien.ToString())
            cmd.Parameters.AddWithValue("@TenNV", nv.TenNhanVien.ToString())
            cmd.Parameters.AddWithValue("@CMND", nv.CMNhanDan.ToString())
            cmd.Parameters.AddWithValue("@DiaChi", nv.QQuan.ToString())
            'cmd.Parameters.AddWithValue("@SoDT", nv.SDienThoai.ToString())
            cmd.Parameters.AddWithValue("@Gioitinh", nv.GTinh.ToString())
            cmd.Parameters.AddWithValue("@Ngaysinh", nv.NSinh.ToString())
            cmd.Parameters.AddWithValue("@Chucvu", nv.CVu.ToString())

            cmd.CommandType = CommandType.Text
            cmd.CommandText = str
            cmd.ExecuteNonQuery()
        Catch ex As Exception
            MessageBox.Show("Error while inserting record on table..." & ex.Message, "Insert Records")
        End Try

    End Sub

    Public Shared Sub DeleteNV_DAO(ByVal nv As NhanVien_DTO)
        Dim conn As SqlConnection
        conn = DataProvider.Connection()
        Dim cmd As SqlCommand = New SqlCommand("delete from NHANVIEN where MaNV = @MaNV", conn)
        cmd.Parameters.AddWithValue("@MaNV", nv.MaNhanVien.ToString())
        cmd.ExecuteNonQuery()
    End Sub
End Class
